/***************************************************************************************************
 *
 * 
 *
 ***************************************************************************************************/

/*
 *
 *
 */
function images_waiting_request(inc)
{
	IMAGE_REQUEST += (inc) ? 1 : (-1);
	$('image_working').display = (IMAGE_REQUEST > 0) ? '' : 'none';
}
/*
 *
 *
 */
function images_set_selected(li)
{
	var lis = $A($("images_list").childNodes);
	lis.each(function(item){
			item.className = (item == li) ? 'selected' : '';
	});
}

/*
 *
 *
 */
function images_img_init(img,item,li)
{
	img.src = "../index.php?c=images&id=" + item.id
	img.name = item.id
	img.className = 'item_img';
	img.onclick = function(){	
		if(li.className == 'selected') return;
		images_set_selected(li);
		images_waiting_request(true);
		var url = 'index.php?c=ajax_images&a=image_set&type=' + ITEM_TYPE +'&itm_id=' + ITEM_ID + '&img_id='+ img.name ; 
		new Ajax.Request(url,{
				method: 'get',
				onSuccess: function(transport){
     			var json = transport.responseText.evalJSON();
					display_success_message(json);
					images_waiting_request(false);
				}
			});
		}
	li.className = (item.selected) ? 'selected' : '';
}


function images_del_init(a,item)
{
	a.innerHTML = '[X]';
  a.onclick = function(){
		if(confirm('Borrar imagen?'))
		{
			images_waiting_request(true);
			var url = 'index.php?c=ajax_images&a=image_del&type=' + ITEM_TYPE + '&img_id=' + item.id; 
			new Ajax.Request(url,{
				method: 'get',
				onSuccess: function(transport){
     			var json = transport.responseText.evalJSON();
					display_success_message(json);
					images_waiting_request(false);
				}
			});
		};
		images_load(ITEM_ID);
	}
}

/*
 *
 *
 */
function images_txt_init(tx,item)
{
	tx.type = 'text';
	tx.value = item.title;
	tx.name = item.id;

	tx.onclick = function(){	
			swap_editable(tx,true)
			tx.old_content = tx.value;
		};

	tx.onblur  = function(){
			swap_editable(tx,false)
			if(tx.old_content == tx.value) return;

			images_waiting_request(true);
			var url = 'index.php?c=ajax_images&a=image_edit&type=' + ITEM_TYPE + '&img_id=' + tx.name + '&title=' + encodeURIComponent(tx.value); 
			new Ajax.Request(url,{
				method: 'get',
				onSuccess: function(transport){
     			var json = transport.responseText.evalJSON();
					display_success_message(json);
					images_waiting_request(false);
				}
			});
		};

	swap_editable(tx,false);
}

/*
 *
 *
 */
function images_refresh(images)
{
	images.each(function(item){
		var li = document.createElement('li');
		var tx = document.createElement('input');
		var im = document.createElement('img');
		var a  = document.createElement('a');


		images_txt_init(tx,item);
		images_del_init(a,item);
		images_img_init(im,item,li);

		li.appendChild(tx);
		li.appendChild(a);
		li.appendChild(im);
		$('images_list').appendChild(li);
	})
}

/*
 *
 *
 */
function images_load(item_id)
{
	var ul = $("images_list");
	var lis = $A(ul.childNodes);
	lis.each(function(item){
			ul.removeChild(item);
			item = null;
		}
	);

	images_waiting_request(true);
	new Ajax.Request('index.php?c=ajax_images&a=images&type='+ ITEM_TYPE +'&id='+ITEM_ID, {
  	method:'get',
  	onSuccess: function(transport){
     	var json = transport.responseText.evalJSON();
			images_refresh(json);
			images_waiting_request(false);
   	}
	});
}

function images_init(item_id)
{
	images_load(item_id);
	$('image_title').value = 'Nueva Imagen';
	$('image_upload_form').action = 'index.php?c=ajax_images&a=image_upload&type='+ ITEM_TYPE +'&itm_id='+ITEM_ID
	$('image_upload_form').onsubmit = function(){
		$('image_upload_iframe').onload = function (){
		 images_load(item_id);
		}
		return true;
	}

	$('image_upload_iframe').style.display = 'none';

}


